﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace EQEmu.Database
{
    public class EQEmuContextFactory : IDisposable
    {
        private List<EQEmuContext> _createdContexts = new List<EQEmuContext>();

        public EQEmuContext Create()
        {
            var connStr = ConfigurationManager.ConnectionStrings["EQEmuEntities"].ConnectionString;
            //var context = new EQEmuContext(connStr);
            var context = Create(connStr);
            _createdContexts.Add(context);
            return context;
        }

        protected virtual EQEmuContext Create(string connectionString)
        {
            return new EQEmuContext(connectionString);
        }

        protected virtual void Dispose(bool disposing)
        {
            foreach (var c in _createdContexts)
            {
                c.Dispose();
            }
        }

        public void Dispose()
        {
            Dispose(true);            
        }
    }
}
